package com.jing.mall.member.service.impl;

import com.jing.mall.member.entity.MemberLevelEntity;
import com.jing.mall.member.exception.PhoneExsitException;
import com.jing.mall.member.exception.UsernameExistException;
import com.jing.mall.member.vo.MemberLoginVo;
import com.jing.mall.member.vo.MemberRegistVo;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jing.mall.common.utils.PageUtils;
import com.jing.mall.common.utils.Query;

import com.jing.mall.member.dao.MemberDao;
import com.jing.mall.member.entity.MemberEntity;
import com.jing.mall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void regist(MemberRegistVo vo) {
        MemberDao memberDao = this.baseMapper;
        MemberEntity memberEntity = new MemberEntity();
        //设置默认等级
        MemberLevelEntity levelEntity = memberDao.getDefaultLevel();
        memberEntity.setLevelId(levelEntity.getId());
        //检查手机号和用户名是否唯一
        memberEntity.setMobile(vo.getPhone());
        checkPhoneUnique(vo.getPhone());
        memberEntity.setUsername(vo.getUserName());
        checkUserNameUnique(vo.getUserName());
        memberDao.insert(memberEntity);
        //密码进行加密处理
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(vo.getPassword());
        memberEntity.setPassword(encode);
    }

    @Override
    public void checkPhoneUnique(String phone) throws PhoneExsitException {
        MemberDao memberDao = this.baseMapper;
        Integer integer = memberDao.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if (integer>0){
            throw new PhoneExsitException();
        }

    }

    @Override
    public void checkUserNameUnique(String userName) throws UsernameExistException {
        MemberDao memberDao = this.baseMapper;
        Integer integer = memberDao.selectCount(new QueryWrapper<MemberEntity>().eq("username", userName));
        if (integer>0){
            throw new UsernameExistException();
        }
    }

    @Override
    public MemberEntity login(MemberLoginVo vo) {
        MemberDao memberDao = this.baseMapper;
        MemberEntity entity = memberDao.selectOne(new QueryWrapper<MemberEntity>().eq("username", vo.getLoginaccount())
                .or().eq("mobile", vo.getLoginaccount())
        );
        if (entity==null){
            return null;
        }else {
            String passwordDB = entity.getPassword();
            BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
            boolean matches = bCryptPasswordEncoder.matches(vo.getPassword(), passwordDB);
            if (matches){
                return entity;
            }else {
                return null;
            }
        }

    }


}
